﻿CMAKE_MINIMUM_REQUIRED(VERSION 3.15.0 FATAL_ERROR)


PROJECT(Comac)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置启用QT编译器
SET(CMAKE_AUTOMOC ON)
SET(CMAKE_AUTOUIC ON)
SET(CMAKE_AUTORCC ON)

# 包含当前目录
SET(CMAKE_INCLUDE_CURRENT_DIR ON)

# 设置项目库文件前缀和后缀，设置项目库文件的输出目录
IF(CMAKE_BUILD_TYPE  MATCHES "Debug")
    SET(PROJECT_BUILD_TYPE_POSTFIX "d")
ELSE()
    SET(PROJECT_BUILD_TYPE_POSTFIX "")
ENDIF()

# 设置编译选项
OPTION(USE_PROJECT_OUTPUT_DIR "Project file output dir" OFF)
# 设置发布类型选项
OPTION(USE_RELEASE_TYPE "${PROJECT_NAME} project release type" OFF)

# 设置发布类型
IF(USE_RELEASE_TYPE)
    SET(PROJECT_RELEASE_TYPE "${USE_RELEASE_TYPE}")
ELSE()
    SET(PROJECT_RELEASE_TYPE "feature") #feature #master #stable #preview
ENDIF()

# 设置项目输出目录
IF(USE_PROJECT_OUTPUT_DIR)
    SET(PROJECT_OUTPUT_DIR ${USE_PROJECT_OUTPUT_DIR})
ELSE()
    SET(PROJECT_OUTPUT_DIR ${PROJECT_SOURCE_DIR}/output)
ENDIF()

SET(PROJECT_OUTPUT_BIN_DIR ${PROJECT_OUTPUT_DIR}/bin${PROJECT_BUILD_TYPE_POSTFIX})
SET(LIBRARY_OUTPUT_PATH ${PROJECT_OUTPUT_BIN_DIR})
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_OUTPUT_BIN_DIR})

#=========================引入外部依赖库=======================================
# 设置第三方库目录
SET(LIBRARY_THIRDPARTY_DIR ${PROJECT_SOURCE_DIR}/ThirdpartyLibs)
#SET(LIBRARY_THIRDPARTY_SHARED_DIR //192.168.1.215/SharedFolder/ThirdpartyLibs)

# 设置第三方依赖库的目录
IF(CMAKE_SYSTEM_NAME MATCHES "Linux")

ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Windows")
SET(Qt5_DIR D:/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5)
    IF(NOT EXISTS ${Qt5_DIR})        
       SET(Qt5_DIR ${LIBRARY_THIRDPARTY_SHARED_DIR}/Qt/Qt5.12.12/5.12.12/msvc2017_64/lib/cmake/Qt5)
    ENDIF()
    SET(SUBDIR_THIRDPARTY_DIR ${LIBRARY_THIRDPARTY_DIR}/Libs/windows-x86_64)
    SET(MODULE_VTK_DIR ${SUBDIR_THIRDPARTY_DIR}/vtk)
    SET(MODULE_QCustomPlot_DIR ${SUBDIR_THIRDPARTY_DIR}/QCustomPlot)
    SET(MODULE_QUAZIP_DIR ${SUBDIR_THIRDPARTY_DIR}/quazip)
    SET(MODULE_PYTHON_DIR ${SUBDIR_THIRDPARTY_DIR}/python37)
    SET(MODULE_OCC_DIR ${SUBDIR_THIRDPARTY_DIR}/occ)
    SET(MODULE_OPENCV_DIR ${SUBDIR_THIRDPARTY_DIR}/opencv)

ENDIF()

MESSAGE(STATUS "{${PROJECT_NAME}} QT_Core目录 = ${Qt5Core_DIR}")

# 检查第三方依赖库目录是否存在
# IF(NOT EXISTS ${Qt5_DIR})        
#     MESSAGE(SEND_ERROR "Please specified third-party library directory!~ Qt5_DIR = ${Qt5_DIR}")
# ENDIF()
#IF(NOT EXISTS ${LIBRARY_THIRDPARTY_SHARED_DIR})        
#    MESSAGE(SEND_ERROR "Please specified third-party library directory!~ LIBRARY_THIRDPARTY_SHARED_DIR = ${LIBRARY_THIRDPARTY_SHARED_DIR}")
#ENDIF()
IF(NOT EXISTS ${MODULE_VTK_DIR})        
    MESSAGE(SEND_ERROR "Please specified Module directory!~  MODULE_VTK_DIR = ${MODULE_VTK_DIR}")
ENDIF()
IF(NOT EXISTS ${MODULE_PYTHON_DIR})
        MESSAGE(SEND_ERROR "Please specified Module directory!~  MODULE_PYTHON_DIR = ${MODULE_PYTHON_DIR}")
ENDIF()
IF(NOT EXISTS ${MODULE_OCC_DIR})        
        MESSAGE(SEND_ERROR "Please specified Module directory!~  MODULE_OCC_DIR = ${MODULE_OCC_DIR}")
ENDIF()
IF(NOT EXISTS ${MODULE_OPENCV_DIR})        
        MESSAGE(SEND_ERROR "Please specified Module directory!~  MODULE_OPENCV_DIR = ${MODULE_OPENCV_DIR}")
ENDIF()
IF(NOT EXISTS ${MODULE_QUAZIP_DIR})        
        MESSAGE(SEND_ERROR "Please specified Module directory!~  MODULE_QUAZIP_DIR = ${MODULE_QUAZIP_DIR}")
ENDIF()

# 设置第三方依赖库的头文件目录
#SET(LIBRARY_VTK_INCLUDE ${MODULE_VTK_DIR}/include)
#SET(LIBRARY_OCC_INCLUDE ${MODULE_OCC_DIR}/include)
#SET(LIBRARY_OPENCV_INCLUDE ${MODULE_OPENCV_DIR}/build/include)

# 设置第三方依赖库库文件目录，并检查是否存在额
IF(CMAKE_SYSTEM_NAME MATCHES "Linux")
    #Set FastCAE Linux Dependencies Libraries Path

    #VTK
    SET(VTK_SO_DIR ${VTK_DIR2}/lib)
    IF(NOT EXISTS ${VTK_SO_DIR})
        MESSAGE(SEND_ERROR "VTK_SO_DIR is null, please specified VTK so directory!~")
    ENDIF()

ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Windows")

    #vld路径设置
    SET(VLD_DIR ${SUBDIR_THIRDPARTY_DIR}/vld-2.5.1)
    INCLUDE_DIRECTORIES(${VLD_DIR}/include)
    FILE(COPY ${VLD_DIR}/bin/vld_x64.dll  DESTINATION ${PROJECT_OUTPUT_BIN_DIR})
    
    #VTK
    #SET(MODULE_VTK_DIR D:/WorkSpace/comac_desk_app/ThirdpartyLibs/Libs/windows-x86_64/vtk)
    IF(NOT EXISTS ${MODULE_VTK_DIR})        
        MESSAGE(SEND_ERROR "Please specified Module directory!~  MODULE_VTK_DIR = ${MODULE_VTK_DIR}")
    ENDIF()

    #OPENCV
    IF(NOT EXISTS ${MODULE_OPENCV_DIR})        
        MESSAGE(SEND_ERROR "Please specified Module directory!~  MODULE_OPENCV_DIR = ${MODULE_OPENCV_DIR}")
    ENDIF()

    # VTK
    SET(LIBRARY_VTK_INCLUDE ${MODULE_VTK_DIR}/include)
    SET(LIBRARY_VTK_LIBDIR ${MODULE_VTK_DIR}/lib${PROJECT_BUILD_TYPE_POSTFIX})
    SET(LIBRARY_VTK_BINDIR ${MODULE_VTK_DIR}/bin${PROJECT_BUILD_TYPE_POSTFIX})
    FILE(GLOB LIBRARY_VTK_LIBS "${LIBRARY_VTK_LIBDIR}/*.lib")
    FILE(GLOB LIBRARY_VTK_BINS "${LIBRARY_VTK_BINDIR}/*.dll")
    IF(NOT LIBRARY_VTK_LIBS)        
        MESSAGE(SEND_ERROR "Please specified lib directory!~ LIBRARY_VTK_LIBDIR=${LIBRARY_VTK_LIBDIR}")
    ENDIF()
    IF(NOT LIBRARY_VTK_BINS)        
        MESSAGE(SEND_ERROR "Please specified bin directory!~ LIBRARY_VTK_BINDIR=${LIBRARY_VTK_BINDIR}")
    ENDIF()

    # OPENCV
    SET(LIBRARY_OPENCV_INCLUDE ${MODULE_OPENCV_DIR}/build/include)
    SET(LIBRARY_OPENCV_LIBDIR ${MODULE_OPENCV_DIR}/build/x64/vc16/lib)
    SET(LIBRARY_OPENCV_BINDIR ${MODULE_OPENCV_DIR}/build/x64/vc16/bin)
    FILE(GLOB LIBRARY_OPENCV_LIBS "${LIBRARY_OPENCV_LIBDIR}/*${PROJECT_BUILD_TYPE_POSTFIX}.lib")
    FILE(GLOB LIBRARY_OPENCV_BINS "${LIBRARY_OPENCV_BINDIR}/*${PROJECT_BUILD_TYPE_POSTFIX}.dll")
    IF(NOT LIBRARY_OPENCV_LIBS)        
        MESSAGE(SEND_ERROR "Please specified lib directory!~ LIBRARY_OPENCV_LIBDIR=${LIBRARY_OPENCV_LIBDIR}")
    ENDIF()
    IF(NOT LIBRARY_OPENCV_BINS)        
        MESSAGE(SEND_ERROR "Please specified bin directory!~ LIBRARY_OPENCV_BINDIR=${LIBRARY_OPENCV_BINDIR}")
    ENDIF()

    # QCustomPlot
    SET(LIBRARY_QCustomPlot_INCLUDE ${MODULE_QCustomPlot_DIR}/include)
    SET(LIBRARY_QCustomPlot_LIBDIR ${MODULE_QCustomPlot_DIR}/lib${PROJECT_BUILD_TYPE_POSTFIX})
    SET(LIBRARY_QCustomPlot_BINDIR ${MODULE_QCustomPlot_DIR}/bin${PROJECT_BUILD_TYPE_POSTFIX})
    FILE(GLOB LIBRARY_QCustomPlot_LIBS "${LIBRARY_QCustomPlot_LIBDIR}/*.lib")
    FILE(GLOB LIBRARY_QCustomPlot_BINS "${LIBRARY_QCustomPlot_BINDIR}/*.dll")
    IF(NOT LIBRARY_QCustomPlot_BINS)        
        MESSAGE(SEND_ERROR "Please specified bin directory!~ LIBRARY_QCustomPlot_BINS=${LIBRARY_QCustomPlot_BINS}")
    ENDIF()
        
    #PYTHON
    SET(LIBRARY_PYTHON_INCLUDE ${MODULE_PYTHON_DIR}/include)
    SET(LIBRARY_PYTHON_LIBDIR ${MODULE_PYTHON_DIR}/lib)
    SET(LIBRARY_PYTHON_BINDIR ${MODULE_PYTHON_DIR}/bin)
    FILE(GLOB LIBRARY_PYTHON_LIBS "${LIBRARY_PYTHON_LIBDIR}/*.lib")
    FILE(GLOB LIBRARY_PYTHON_BINS "${LIBRARY_PYTHON_BINDIR}/*.dll")
    IF(NOT LIBRARY_PYTHON_LIBS)        
        MESSAGE(SEND_ERROR "Please specified lib directory!~ LIBRARY_PYTHON_LIBDIR=${LIBRARY_PYTHON_LIBDIR}")
    ENDIF()
    IF(NOT LIBRARY_PYTHON_BINS)        
        MESSAGE(SEND_ERROR "Please specified bin directory!~ LIBRARY_PYTHON_BINDIR=${LIBRARY_PYTHON_BINDIR}")
    ENDIF()

    #OCC
    SET(LIBRARY_OCC_INCLUDE ${MODULE_OCC_DIR}/include)
    SET(LIBRARY_OCC_LIBDIR ${MODULE_OCC_DIR}/lib)
    SET(LIBRARY_OCC_BINDIR ${MODULE_OCC_DIR}/bin${PROJECT_BUILD_TYPE_POSTFIX})
    FILE(GLOB LIBRARY_OCC_LIBS "${LIBRARY_OCC_LIBDIR}/*.lib")
    FILE(GLOB LIBRARY_OCC_BINS "${LIBRARY_OCC_BINDIR}/*.dll")
    IF(NOT LIBRARY_OCC_LIBS)        
        MESSAGE(SEND_ERROR "Please specified lib directory!~ LIBRARY_OCC_LIBDIR=${LIBRARY_OCC_LIBDIR}")
    ENDIF()
    IF(NOT LIBRARY_OCC_BINS)        
        MESSAGE(SEND_ERROR "Please specified bin directory!~ LIBRARY_OCC_BINDIR=${LIBRARY_OCC_BINDIR}")
    ENDIF()

    #QUAZIP
    SET(LIBRARY_QUAZIP_INCLUDE ${MODULE_QUAZIP_DIR}/include)
    SET(LIBRARY_QUAZIP_LIBDIR ${MODULE_QUAZIP_DIR}/lib${PROJECT_BUILD_TYPE_POSTFIX})
    SET(LIBRARY_QUAZIP_BINDIR ${MODULE_QUAZIP_DIR}/bin${PROJECT_BUILD_TYPE_POSTFIX})
    FILE(GLOB LIBRARY_QUAZIP_LIBS "${LIBRARY_QUAZIP_LIBDIR}/*.lib")
    FILE(GLOB LIBRARY_QUAZIP_BINS "${LIBRARY_QUAZIP_BINDIR}/*.dll")
    IF(NOT LIBRARY_QUAZIP_LIBS)        
        MESSAGE(SEND_ERROR "Please specified lib directory!~ LIBRARY_QUAZIP_LIBDIR=${LIBRARY_QUAZIP_LIBDIR}")
    ENDIF()
    IF(NOT LIBRARY_QUAZIP_BINS)        
        MESSAGE(SEND_ERROR "Please specified bin directory!~ LIBRARY_QUAZIP_BINDIR=${LIBRARY_QUAZIP_BINDIR}")
    ENDIF()

ENDIF()


ADD_SUBDIRECTORY(BaseModule)
ADD_SUBDIRECTORY(BaseWidget)
ADD_SUBDIRECTORY(CommandManager)
ADD_SUBDIRECTORY(DataManager)
ADD_SUBDIRECTORY(ScriptManager)
ADD_SUBDIRECTORY(ProjectManager)
ADD_SUBDIRECTORY(ComacMainProgram)
ADD_SUBDIRECTORY(PostProcessing)
ADD_SUBDIRECTORY(GenerateReport)


# 复制第三方依赖库文件
MESSAGE(STATUS "Start copying third-party dependent library files.")
FILE(COPY ${LIBRARY_VTK_BINS}
          ${LIBRARY_PYTHON_BINS}
          ${LIBRARY_OCC_BINS}
          ${LIBRARY_OPENCV_BINS}
          ${LIBRARY_QUAZIP_BINS}
          DESTINATION ${PROJECT_OUTPUT_BIN_DIR})

# 复制qt插件文件
MESSAGE(STATUS "Start copying qt plugins files.")
FILE(COPY ${Qt5_DIR}/../../../plugins/platforms
          ${Qt5_DIR}/../../../plugins/imageformats
          ${Qt5_DIR}/../../../plugins/platformthemes
          ${Qt5_DIR}/../../../plugins/styles
          #${Qt5_DIR}/../../../plugins/audio
          DESTINATION ${PROJECT_OUTPUT_BIN_DIR}/plugins)

# 复制qt库文件
MESSAGE(STATUS "Start copying resource files and QT library files.")
FILE(COPY ${Qt5_DIR}/../../../bin/Qt5Core${PROJECT_BUILD_TYPE_POSTFIX}.dll
          ${Qt5_DIR}/../../../bin/Qt5Gui${PROJECT_BUILD_TYPE_POSTFIX}.dll
          ${Qt5_DIR}/../../../bin/Qt5Widgets${PROJECT_BUILD_TYPE_POSTFIX}.dll
          ${Qt5_DIR}/../../../bin/Qt5Xml${PROJECT_BUILD_TYPE_POSTFIX}.dll
          #${Qt5_DIR}/../../../bin/Qt5Network${PROJECT_BUILD_TYPE_POSTFIX}.dll
          ${Qt5_DIR}/../../../bin/Qt5PrintSupport${PROJECT_BUILD_TYPE_POSTFIX}.dll
          #${Qt5_DIR}/../../../bin/Qt5Positioning${PROJECT_BUILD_TYPE_POSTFIX}.dll
          #${Qt5_DIR}/../../../bin/Qt5Multimedia${PROJECT_BUILD_TYPE_POSTFIX}.dll
          ${Qt5_DIR}/../../../bin/libEGL${PROJECT_BUILD_TYPE_POSTFIX}.dll
          ${Qt5_DIR}/../../../bin/libGLESv2${PROJECT_BUILD_TYPE_POSTFIX}.dll
          #${Qt5_DIR}/../../../resources
          DESTINATION ${PROJECT_OUTPUT_BIN_DIR})


# 复制python配置文件
MESSAGE(STATUS "Start copying Config Files.")
FILE(COPY ${PROJECT_SOURCE_DIR}/Resources/ConfigFiles/qt.conf
          ${PROJECT_SOURCE_DIR}/Resources/scripts
          DESTINATION ${PROJECT_OUTPUT_BIN_DIR})


# 复制python脚本文件、几何模型文件和配置文件
MESSAGE(STATUS "Start copying example Files.")
FILE(COPY 
          #${PROJECT_SOURCE_DIR}/Resources/examples
          ${PROJECT_SOURCE_DIR}/Resources/StandardFileLibrary
          ${PROJECT_SOURCE_DIR}/Resources/ReportTemplateData
          ${PROJECT_SOURCE_DIR}/Resources/user
          DESTINATION ${PROJECT_OUTPUT_DIR})


# 设置解压程序路径
FIND_PACKAGE(Git QUIET)
SET(GIT_PATH_VAR ${GIT_EXECUTABLE})
#   从后向前裁剪路径，找到 Git 安装目录
STRING(REGEX REPLACE "(.*Git).*" "\\1" GIT_INSTALL_DIR "${GIT_PATH_VAR}")
SET(PATH_UNZIP "${GIT_INSTALL_DIR}/usr/bin/unzip.exe")
IF(NOT EXISTS ${PATH_UNZIP})
    MESSAGE(SEND_ERROR "unzip path resolution error. GIT_INSTALL_DIR = ${GIT_INSTALL_DIR}.")
ENDIF()

# 复制大型第三方库文件
MESSAGE(STATUS "Start copying large third-party library files.")
IF(NOT EXISTS ${PROJECT_OUTPUT_DIR}/python37)        
    MESSAGE(STATUS "      Start copying python37 program Files.")
ELSE()
    FILE(REMOVE_RECURSE "${PROJECT_OUTPUT_DIR}/python37")
    MESSAGE(STATUS "      Delete existing output/python37 Files")
ENDIF()
# 复制python37的压缩文件
FILE(COPY ${LIBRARY_THIRDPARTY_DIR}/Libs/windows-x86_64/python37.zip DESTINATION ${PROJECT_OUTPUT_DIR})

# 解压python37的压缩文件
SET(CMD_PYTHON37 ${PATH_UNZIP} ${PROJECT_OUTPUT_DIR}/python37.zip -d ${PROJECT_OUTPUT_DIR})
EXECUTE_PROCESS(COMMAND ${CMD_PYTHON37} RESULT_VARIABLE UNZIP_RESULT  OUTPUT_QUIET)
IF(NOT UNZIP_RESULT EQUAL 0)
    MESSAGE(SEND_ERROR "python37.zip 解压失败。 错误码: ${UNZIP_RESULT}")
ENDIF()
